
package com.twohalf.mifty.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.twohalf.mifty.service.gen.MiscService.Iface;
import com.twohalf.mifty.utils.Tools;

import club.jmint.mifty.dao.Sql;
import club.jmint.mifty.service.MiftyService;
import club.jmint.mifty.utils.CrossLog;

public class MiscServiceImpl extends MiftyService implements Iface {
	private String dbName = "db_twohalf";
	private String sliderResourceTable = dbName+".t_sliders_resouce";
	private Sql ddl = Sql.getInstance();

	/**
	 * Create slider resource
	 * @param sponsor_id
	 * @param fields (sponsor_id,picture_url,link_url,description)
	 * @return id
	 */
	@Override
	public Map<String, String> sliderResCreate(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String sponsor_id = params.get("sponsor_id");
		
		int erows = ddl.sqlCreate(sliderResourceTable, params);
		if (erows<0){
			CrossLog.logger.error("slider resource add failed.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_DB_OPERATION_FAILED.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_DB_OPERATION_FAILED.getInfo());
			return resultMap;			
		}
		else if (erows==0){
		}
		else {
			CrossLog.logger.info("added slider resource: " + sponsor_id);
		}
		
		//query and get the id
		resultMap.put("id", Integer.toString(ddl.sqlLastInsertId()));
		return resultMap;

	}

	/**
	 * Update slider resource
	 * @param sponsor_id
	 * @param id
	 * @param fields (sponsor_id,picture_url,link_url,description)
	 * @return errorCode
	 */
	@Override
	public Map<String, String> sliderResUpdate(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String sponsor_id = params.get("sponsor_id");
		String id = params.get("id");
		String condition = "id='"+id+"'";
		condition += " and sponsor_id='"+sponsor_id+"'";
		
		int erows = ddl.sqlUpdate(sliderResourceTable, params, condition);
		if (erows<0){
			CrossLog.logger.error("slider resource update failed.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_DB_OPERATION_FAILED.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_DB_OPERATION_FAILED.getInfo());
			return resultMap;			
		} else if (erows==0){
			CrossLog.logger.warn("slider not exists: " + id);
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;			
		} else {
			CrossLog.logger.info("updated slider resource: " + id);
		}
		
		resultMap.put("id", id);
		return resultMap;
	}

	/**
	 * Query slider resource by sponsor
	 * @param sponsor_id
	 * @param fields (id,sponsor_id,picture_url,link_url,description)
	 */
	@Override
	public Map<String, String> sliderResInfoListQuery(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String sponsor_id = params.get("sponsor_id");
		String condition = "sponsor_id='"+sponsor_id+"'";
		
		String fields = params.get("fields");
		String[] fieldlist = fields.split(",");
		
		List<Object[]> list = ddl.sqlSelect(sliderResourceTable, fields, condition);
		if (list.isEmpty()){
			CrossLog.logger.warn("sponsor slider not exists.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;			
		}
		CrossLog.logger.info("query all sponsor slider resource.");
		
		Object[] objarr = null;
		JsonArray ja = new JsonArray();
		JsonObject jo = null;
		for (int idx=0;idx<list.size();idx++){
			objarr = list.get(idx);
			jo = new JsonObject();
			for (int i=0;i<fieldlist.length;i++){
				jo.addProperty(fieldlist[i],Sql.getSqlResultFieldAsString(objarr[i]));
			}
			ja.add(jo);
		}
		resultMap.put("resource", ja.toString());
		return resultMap;
	}

	/**
	 * Delete slider resource
	 * @param sponsor_id
	 * @param id(optional)
	 * @return errorCode
	 */
	@Override
	public Map<String, String> sliderResDelete(Map<String, String> params) throws TException {
		HashMap<String, String> resultMap = new HashMap<String, String>();
		String id = params.get("id");
		String sponsor_id = params.get("sponsor_id");
		String condition = "sponsor_id='"+sponsor_id+"'";
		if (id!=null && !id.isEmpty()){
			condition += " and id='"+id+"'";
		}
		
		int erows = ddl.sqlDelete(sliderResourceTable, condition);
		if (erows<0){
			CrossLog.logger.error("slider resource delete failed.");
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_DB_OPERATION_FAILED.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_DB_OPERATION_FAILED.getInfo());
			return resultMap;			
		}
		else if (erows==0){
			CrossLog.logger.warn("slider resource not exists: " + id);
			resultMap.put("errorCode", Integer.toString(ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getCode()));
			resultMap.put("errorInfo", ErrorCode.TWOHALF_OBJECT_NOT_EXIST.getInfo());
			return resultMap;
		}
		else{
			CrossLog.logger.info("deleted slider resource: " + id);
		}
		
		return resultMap;
	}

}

